package zhieasy.cn.xpu_helper.controller.initiation;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhieasy.cn.common.utils.ResultVOUtil;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.dto.initiation.TaskDTO;
import zhieasy.cn.dto.initiation.TaskGroupDTO;
import zhieasy.cn.dto.initiation.TaskGroupVO;
import zhieasy.cn.service.initiation.TaskService;
import java.util.List;

/**
 * @author sofency
 * @date 2020/8/2 7:51
 * @package IntelliJ IDEA
 * @description
 */
@RestController
@RequestMapping("/initiation")
@Api(tags = "开学地图与任务相关的api")
public class TaskController {
    @Autowired
    TaskService taskService;
    /**
     * 根据Id查询任务信息
     * @param groupId
     * @return
     */
    @Deprecated
    @ApiOperation(value = "根据任务组id查询任务组信息",notes = "根据任务组id查询任务组信息")
    @RequestMapping(value = "/task",method = RequestMethod.GET)
    public ResultVO selectTaskById(@ApiParam("任务组的id号") String groupId){
        List<TaskDTO> objects = taskService.getGroup(groupId);
        return ResultVOUtil.success(objects);
    }

    /**
     * 根据任务组Id删除任务组
     * @param groupId
     * @return
     */
    @ApiOperation(value = "根据任务组删除id",notes = "根据任务组删除id")
    @RequestMapping(value = "/task",method = RequestMethod.DELETE)
    public ResultVO deleteTaskById(String groupId) throws Exception {
        Boolean flag = taskService.deleteTask(groupId);
        return flag?ResultVOUtil.success():ResultUtils.error();
    }


    /**
     * 添加任务
     * @param taskGroupVO
     * @return
     */
    @ApiOperation(value = "添加任务" ,notes = "添加任务")
    @RequestMapping(value = "/task",method = RequestMethod.PUT)
    public ResultVO addTask(@ApiParam("任务组信息")@RequestBody TaskGroupVO taskGroupVO){
        Boolean aBoolean = taskService.insertTask(taskGroupVO.getTaskGroup(), taskGroupVO.getTaskInfos());
        return aBoolean?ResultVOUtil.success():ResultUtils.error("抱歉你不是该任务的创建者");
    }

    /**
     * 获取学院下的任务组信息
     * @return
     */
    @ApiOperation(value="获取学院下的任务组信息",notes = "获取学院组下的任务组信息")
    @GetMapping("/tasks/isFirst/{isFirst}")
    public ResultVO getTasks(@ApiParam("是否是开学任务")@PathVariable("isFirst") Integer isFirst){//传入用户的唯一id
        List<TaskGroupDTO> objects = taskService.getTask(isFirst);
        return ResultVOUtil.success(objects);
    }

    /**
     * 完成任务
     * @param groupId
     * @param taskId
     * @return
     */
    @ApiOperation(value="完成任务",notes = "完成任务")
    @GetMapping("/finishTask")
    public ResultVO finishTask(@ApiParam("经度")@RequestParam("lo") Double lo,
                               @ApiParam("纬度") @RequestParam("io") Double io,
                               @ApiParam("任务组id") @RequestParam("groupId") String groupId,
                               @ApiParam("任务id") @RequestParam("taskId") String taskId){
        Boolean aBoolean = taskService.updateStudentTask(lo, io, taskId, groupId);
        return !aBoolean?ResultUtils.error():ResultUtils.success();
    }


    /***
     * 更新任务的信息
     */
    @ApiOperation(value="更新任务组的信息",notes = "更新任务组的信息")
    @RequestMapping(value = "/task",method = RequestMethod.POST)
    public ResultVO updateTask(@ApiParam("任务组信息")@RequestBody TaskGroupVO taskGroupVO){
        taskService.updateTask(taskGroupVO.getTaskGroup(),taskGroupVO.getTaskInfos());
        return ResultUtils.success();
    }


}
